###############################################################################
# Copyright (C) 2010 Chris Wroe
#
# Modifications to the initial code base are copyright of their respective
# authors, or their employers as appropriate.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307
##############################################################################

require "rubygems"
require "neo4j"



class LoadSnomedComponent

  @module_config=nil

  def initialize
    self.neo_index_node_setup
  end

  #deprecated
  def find_node_ids(filter,index)
    #TODO will need to deal with linking to the right concept in the right release and module using filter
    hits = index.find filter

    return hits.collect {|h|h[:id]}
  end

  #deprecated
  def get_node_id_for_next_field(ext_id,index,filter,id_field)
    filter[id_field]=ext_id

    neo_node_ids = self.find_node_ids(filter, index)
    raise "cross reference id not found #{ext_id}" if not neo_node_ids.size==1
    return neo_node_ids[0].to_i

  end

  def neo_index_node_setup
    
    instance_running = Neo4j.running?
    in_transaction = Neo4j::Transaction.running?
    Neo4j.start unless instance_running

    Neo4j::Transaction.new unless in_transaction
    @index_node_id = Neo4j.ref_node.rels.outgoing(:index_node).nodes.first.neo_id

    Neo4j::Transaction.finish unless in_transaction
    Neo4j.stop unless instance_running

  end

  def module_config=(config)
    @module_config=config
  end

  def module_config
    @module_config
  end

  def release
    @module_config['release']
  end

  def read_row(f)
    line = f.gets
    return line
  end

end


